package com.sdhs.analyze.service;

import com.alibaba.fastjson.JSONObject;
import com.sdhs.analyze.bean.org.OrgInfo;
import com.sdhs.analyze.common.BizConstants;
import com.sdhs.analyze.dao.org.OrgInfoMapper;
import com.sdhs.analyze.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class OrgService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrgInfoMapper orgInfoMapper;

    /**
     * 获取机构信息
     * @param orgId
     * @param orgType
     * @return
     */
    public OrgInfo getOrgInfo(String orgId, int orgType) {
        String orgInfoKey = BizConstants.REDIS_KEY_PRE_ORG_INFO + orgType + ":" + orgId;
        String jsonString = redisUtil.get(orgInfoKey);
        OrgInfo orgInfo = null;
        if(jsonString == null) {
            /* 从数据库取 */
            switch(orgType) {
                case BizConstants.ORG_TYPE_GANTRY_FRONT:
                    orgInfo = orgInfoMapper.getOrgInfoByGantryFront(orgId);
                    break;
                case BizConstants.ORG_TYPE_GANTRY:
                case BizConstants.ORG_TYPE_GANTRY_BACK:
                    orgInfo = orgInfoMapper.getOrgInfoByGantryBack(orgId);
                    break;
                case BizConstants.ORG_TYPE_SUBCENTER:
                    orgInfo = orgInfoMapper.getOrgInfoBySubCenter(orgId);
                    break;
                case BizConstants.ORG_TYPE_ROAD:
                    orgInfo = orgInfoMapper.getOrgInfoByRoad(orgId);
                    break;
                case BizConstants.ORG_TYPE_OWNER:
                    orgInfo = orgInfoMapper.getOrgInfoByOwner(orgId);
                    break;
                default:
                    break;
            }
            if (orgInfo != null) {
                /* 缓存 */
                redisUtil.setEx(orgInfoKey, JSONObject.toJSONString(orgInfo), 365, TimeUnit.DAYS);
            }
        }
        else {
            orgInfo = JSONObject.parseObject(jsonString, OrgInfo.class);
        }
        return orgInfo;
    }
    /**
     * 获取门架后台及其管理的门架的机构信息
     * @param orgId
     * @return
     */
    public HashMap<String, OrgInfo> getGantrySystemOrgInfoMap(String orgId) {
        String orgInfoKey = BizConstants.REDIS_KEY_PRE_GANTRY_SYSTEM_ORG_INFO_MAP + orgId;
        String jsonString = redisUtil.get(orgInfoKey);
        Map<String, OrgInfo> orgInfoMap = null;
        if(jsonString == null) {
            orgInfoMap = new HashMap<>();
            /* 从数据库取 */
            List<OrgInfo> orgInfoList = orgInfoMapper.getGantrySystemOrgInfo(orgId);
            if (orgInfoList != null) {
                for(OrgInfo orgInfo : orgInfoList) {
                    orgInfoMap.put(orgInfo.getOrgId(), orgInfo);
                }
                /* 缓存 */
                redisUtil.setEx(orgInfoKey, JSONObject.toJSONString(orgInfoMap), 365, TimeUnit.DAYS);
            }
        }
        else {
            orgInfoMap = (Map<String, OrgInfo>)JSONObject.parseObject(jsonString, HashMap.class);
            for (Map.Entry<String, OrgInfo> element : orgInfoMap.entrySet()) {
                Object tmp = element.getValue();
                tmp = JSONObject.parseObject(JSONObject.toJSONString(tmp), OrgInfo.class);
                orgInfoMap.put(element.getKey(), (OrgInfo)tmp);
            }
        }
        return (HashMap)orgInfoMap;
    }

    /**
     * 获取所有门架后台
     */
    public List<OrgInfo> getGantryBackList(){
        return orgInfoMapper.getGantryBackList();
    }
}
